package com.agile.leetcode.http.client;

import com.agile.leetcode.http.protocol.Request;
import com.agile.leetcode.http.protocol.Response;
import com.agile.leetcode.http.util.Encode;
import com.agile.leetcode.http.util.ProtocolUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/24 22:15
 */
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 8081);
        Request request = new Request();
        request.setCommand("陈彰坤");
        request.setCommandLength(request.getCommand().length());
        request.setCode(Encode.UTF8);
        OutputStream outputStream = socket.getOutputStream();
        // 发送请求
        ProtocolUtil.writeRequest(outputStream, request);

        // 拿到输出流
        InputStream inputStream = socket.getInputStream();
        // 反序列化
        Response response = ProtocolUtil.readResponse(inputStream);
        // 拿到内容
        System.out.println("response=" + response.toString());
    }
}
